package com.fs.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fs.pojo.Class;
import com.fs.service.ClassService;
import com.fs.utils.BeanFactories;
import com.fs.utils.JsonData;
import com.fs.utils.ResponseJSONUtil;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ClassServlet", value = "/class/*")
public class ClassServlet extends HttpServlet {

    private ClassService classService = BeanFactories.getBean(ClassService.class);
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");

        if("/createClass".equals(request.getPathInfo())){
            createClass(request,response);
        }else if("/delClass".equals(request.getPathInfo())){
            delClass(request,response);
        }else if("/updateClass".equals(request.getPathInfo())){
            updateClass(request,response);
        }else if("/deleteStudentInClass".equals(request.getPathInfo())){
            deleteStudentInClass(request,response);
        }else if("/queryClassByCourseIdAndstudentId".equals(request.getPathInfo())){
            queryClassByCourseIdAndstudentId(request,response);

}



    }

    private void queryClassByCourseIdAndstudentId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{


        String studentId = request.getParameter("studentId");
        Class cla = classService.queryClassByCourseIdAndstudentId(studentId);
        if(cla!=null){
            ResponseJSONUtil.responseJSON(JsonData.buildSuccess(cla), response);
        } else {
            ResponseJSONUtil.responseJSON(JsonData.buildError("error！"), response);
        }

    }

    private void deleteStudentInClass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        JSONObject data = JSONObject.parseObject(IOUtils.toString(req.getInputStream(),"utf-8"));
        String classId = data.getString("classId");
        JSONArray idArr =data.getJSONArray("idArr");
        int result =classService.deleteStudentInClass(classId,idArr);
        if (result>0){
            resp.getWriter().print(JSONObject.toJSONString(new JsonData(1,"","删除成功!")));
        }else{
            resp.getWriter().print(JSONObject.toJSONString(new JsonData(0,"","删除失败!请重试!")));
        }

    }

    private void updateClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{




        String id = request.getParameter("id");
        String className = request.getParameter("className");
        String introduce = request.getParameter("introduce");
        String code = request.getParameter("code");
        Class cla = new Class();
        cla.setId(id);
        cla.setClassName(className);
        cla.setIntroduce(introduce);
        cla.setCode(code);
        boolean b = classService.updateClass(cla);
        if (b) {
            ResponseJSONUtil.responseJSON(JsonData.buildSuccess(b), response);
        } else {
            ResponseJSONUtil.responseJSON(JsonData.buildError("error！"), response);
        }
        
    }

    private void delClass(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException{
        String classId = request.getParameter("classId");
        boolean b = classService.delClass(classId);
        if (b) {
            ResponseJSONUtil.responseJSON(JsonData.buildSuccess(b), response);
        } else {
            ResponseJSONUtil.responseJSON(JsonData.buildError("error！"), response);
        }

    }

    private void createClass(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException{


        String courseId = request.getParameter("courseId");
        String className = request.getParameter("className");
        String introduce = request.getParameter("introduce");
        String code = request.getParameter("code");
        Class cla = new Class();
        cla.setCourseId(courseId);
        cla.setClassName(className);
        cla.setIntroduce(introduce);
        cla.setCode(code);
        Class aClass = classService.createClass(cla);
        if(aClass != null) {
            ResponseJSONUtil.responseJSON(JsonData.buildSuccess(aClass), response);
        }else{
            ResponseJSONUtil.responseJSON(JsonData.buildError("添加失败！"),response);
        }




    }
}
